package com.jcw.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jcw.common.R;
import com.jcw.domain.OrderDetail;
import com.jcw.domain.Orders;
import com.jcw.domain.ShoppingCart;
import com.jcw.service.OrderDetailService;
import com.jcw.service.OrdersService;
import com.jcw.mapper.OrdersMapper;
import com.jcw.service.ShoppingCartService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
* @author JCW
* @description 针对表【orders(订单表)】的数据库操作Service实现
* @createDate 2023-03-30 11:19:26
*/
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders>
    implements OrdersService{

    @Resource
    private HttpSession session;

    @Resource
    private OrderDetailService orderDetailService;

    @Resource
    private ShoppingCartService shoppingCartService;

    @Override
    public R<Page> pagination(Integer page, Integer pageSize) {
        Long userID = (Long) session.getAttribute("user");
        Page pageInfo = new Page(page,pageSize);
        query().eq("user_id",userID).page(pageInfo);

        String s = pageInfo.getRecords().toString();
        System.out.println(s);

        return R.success(pageInfo);
    }

    @Override
    public R<String> submit(Orders orders) {

        Long userId = (Long) session.getAttribute("user");
        List<ShoppingCart> list = shoppingCartService.query().eq("user_id", userId).list();
        BigDecimal amount = new BigDecimal(0);
        String orderID = RandomUtil.randomNumbers(16);
        orders.setNumber(orderID);
        orders.setStatus(4);
        orders.setUserId(userId);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setAmount(amount);

        List<OrderDetail> orderDetailList = new ArrayList<>();
        for (ShoppingCart cart : list) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setAmount(cart.getAmount());
            orderDetail.setOrderId(Long.valueOf(orderID));
            orderDetail.setName(cart.getName());
            orderDetail.setImage(cart.getImage());
            orderDetail.setDishId(cart.getDishId());
            orderDetail.setSetmealId(cart.getSetmealId());
            orderDetail.setDishFlavor(cart.getDishFlavor());
            orderDetailList.add(orderDetail);

            BigDecimal decimal = cart.getAmount();
            amount = amount.add(decimal);
        }

        boolean save = save(orders);
        if (save){
            boolean saved = orderDetailService.saveBatch(orderDetailList);
            if (saved){
                boolean removed = shoppingCartService.update().eq("user_id", userId).remove();
                if (removed){
                    return R.success("结算成功");
                }
            }
        }
        return R.error("结算失败");
    }
}




